package com.brook.springcloud.controller;

import com.brook.springcloud.pojo.Dept;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Brook
 */
@RestController
public class DeptConsumerController {
    //消费者不应该有service
    //restTemplate有很多方法供我们调用，需要注册这个到spring中

    @Resource
    private RestTemplate restTemplate; //提供多种便捷访问远程http请求的方法，简单spring提供的restful模板

    /**
     *  //通过ribbon实现负载的时候这里应该配置变量，通过服务名称访问
     *     //private static final String REST_URL_PREFIX = "http://localhost:8001";
     */
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    @RequestMapping("/consumer/dept/add")
    public Boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class);
    }

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
    }

    @RequestMapping("/consumer/dept/get/list")
    public List<Dept> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list/", List.class);
    }

}
